package cn.net.ecode.tools.ecodeplatformmaker.view;

import java.util.List;
import java.util.Map;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.layout.GridData;

import cn.net.ecode.tools.ecodeplatformmaker.dialog.EditConn;
import cn.net.ecode.tools.ecodeplatformmaker.model.ListConn;
import cn.net.ecode.tools.ecodeplatformmaker.model.ListConnContentProvider;
import cn.net.ecode.tools.ecodeplatformmaker.model.ListConnLabelProvider;

import org.eclipse.swt.widgets.Tree;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;

public class ConnTree extends ViewPart {

	public static final String ID = "cn.net.ecode.tools.ecodeplatformmaker.view.ConnTree"; //$NON-NLS-1$
	TreeViewer treeViewer;
	public ConnTree() {
	}

	/**
	 * Create contents of the view part.
	 * @param parent
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NONE);
		
		treeViewer = new TreeViewer(container, SWT.BORDER);
		treeViewer.setLabelProvider(new ListConnLabelProvider());
		treeViewer.setContentProvider(new ListConnContentProvider());
		treeViewer.setInput(new ListConn());
		treeViewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(final DoubleClickEvent arg0) {
				IStructuredSelection selection = (IStructuredSelection) arg0
						.getViewer().getSelection();
				//Card card = (Card) selection.getFirstElement();
				Map conn = (Map) selection.getFirstElement();
				EditConn pd = new EditConn(null);
				pd.init(conn);
				pd.open();
			}
		});
		Tree tree = treeViewer.getTree();
		tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));   
		tree.setBounds(0, 0, 2000, SWT.BOTTOM);
		createActions();
		initializeToolBar();
		initializeMenu();
	}

	/**
	 * Create the actions.
	 */
	private void createActions() {
		// Create the actions
	}

	/**
	 * Initialize the toolbar.
	 */
	private void initializeToolBar() {
		IToolBarManager toolbarManager = getViewSite().getActionBars()
				.getToolBarManager();
	}

	/**
	 * Initialize the menu.
	 */
	private void initializeMenu() {
		IMenuManager menuManager = getViewSite().getActionBars()
				.getMenuManager();
	}

	@Override
	public void setFocus() {
		// Set the focus
	}
	
	public void refreshData(){		
		treeViewer.setInput(new ListConn());
		treeViewer.refresh();
	}
}
